
package com.kanjian.music.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.android.volley.toolbox.NetworkImageView;
import com.kanjian.music.R;
import com.kanjian.music.activity.MusicianHomeActivity;
import com.kanjian.music.constants.IntentConstants;
import com.kanjian.music.entity.Banner;
import com.kanjian.music.volley.VolleyQueue;

public class BannerFragment extends BaseFragment {

    public static BannerFragment newInstance(Banner banner) {
        BannerFragment fragment = new BannerFragment();
        Bundle b = new Bundle();
        b.putSerializable("banner", banner);
        fragment.setArguments(b);
        return fragment;
    }

    private RelativeLayout mRootView;

    private NetworkImageView mNetworkImageView;

    private Banner mBanner;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        mBanner = (Banner) getArguments().getSerializable("banner");
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = (RelativeLayout) inflater.inflate(R.layout.fragment_banner, container,
                    false);
            mNetworkImageView = (NetworkImageView) mRootView.findViewById(R.id.banner_imgview);
            mNetworkImageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mBanner.mBannerType != null) {
                        if (mBanner.mBannerType.equalsIgnoreCase(Banner.BANNER_TYPE_USER)) {
                            Intent intent = new Intent();
                            intent.setClass(getActivity(), MusicianHomeActivity.class);
                            intent.putExtra(IntentConstants.MUSICIANID, mBanner.mTheId);
                            startActivity(intent);
                        } else if (mBanner.mBannerType.equalsIgnoreCase(Banner.BANNER_TYPE_SONG)) {

                        }
                    }
                }
            });
        }
        return mRootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mRootView = null;
    }

    @Override
    public void onResume() {
        super.onResume();
        mNetworkImageView.setDefaultImageResId(R.drawable.ic_launcher);
        mNetworkImageView.setErrorImageResId(R.drawable.ic_launcher);
        mNetworkImageView.setImageUrl(mBanner.mPicUrl, VolleyQueue.getImageLoader());
    }

}
